import fs from "fs";
import path, { resolve } from "path";
import { VideoExt, screenshotPath } from "../utils/const";
import Ffmpeg from "fluent-ffmpeg";
import { operateFile } from "./operateFile";
import { fileLog } from "@/log";
import { base642String, string2Base64 } from "./base64"
import {MediaPromise} from '../interface/interface'

/**
 * @description: 处理所有的视频文件
 * @param {string} mediaPath
 * @return {*}
 * @author: tao wenbo 2419421809@qq.com
 */
export const operateVideoFile = async (mediaPath: string) => {
    // await operateFile(mediaPath, VideoExt, getScreenshot);
};





/**
 * @description: 获取视频截图
 * @param {string} mediaPath 多媒体的路径
 * @return {*}
 * @author: tao wenbo 2419421809@qq.com
 */
export async function getScreenshot(mediaPath: string, ext: string): Promise<MediaPromise> {
    let dir = path.dirname(mediaPath).replace(path.resolve(__dirname, "../../../"), "");
    return new Promise((resolve, reject) => {
        // 先获取视频信息
        Ffmpeg.ffprobe(mediaPath, function (err: any, metadata: any) {
            if (err) {
                console.log(err);
                reject( err )
            } else {
                // 
                // 获取宽高比
                let arr = metadata.streams;
                let duration = 0
                let height, width;
                for (let i of arr) {
                    if (i.height) {
                        height = i.height;
                        width = i.width;
                        duration = Math.floor(i.duration * 1000)
                        break;
                    }
                }
                let bi = width / height;
                let folder = path.join(screenshotPath, dir);
                fs.mkdir(folder, { recursive: true }, (err) => {
                    if (err) {
                        reject(`创建目录失败, ${folder}`);
                    }
                    
                    try {
                        let filename = path.basename( mediaPath, ext )
                        filename = string2Base64( filename ).replace( /\//g, '_' )
                        console.log(filename);
                        // 切图
                        Ffmpeg(mediaPath)
                            .on("end", function () {
                                resolve( {
                                    path: path.resolve(folder, `${filename}.png`),
                                    duration
                                } )
                            })
                            .takeScreenshots({
                                timestamps: ["50%"],
                                filename: filename,
                                folder,
                                size: `${Math.floor(1024 * bi)}x1024`,
                            }).on( "error", function(err){
                                console.log(mediaPath);
                                reject( `${mediaPath} 处理时 Ffmpeg has been killed`)
                            } );
                    } catch (error) {
                        reject(`视频切图失败, ${folder}`);
                    }
                });
            }
        });
    });
}



